﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Data;
using System.Text;

namespace simpleHotel.Web.UserControls
{
    public partial class ucWeather : System.Web.UI.UserControl
    {
        YahooWeather w1;
        string result = "";
        XElement input;
        DataTable dt = new DataTable();
        
        Dictionary<string, string> dictProvinces = new Dictionary<string, string> 
        { 
            { "VMXX0007", "Hồ Chí Minh" },
            { "VMXX0006", "Hà Nội" },
            { "VMXX0028", "Đà Nẵng" },
            { "VMXX0009", "Huế" },
            { "VMXX0032", "Phú Quốc" }
        };
        string[] strProvineArr = new string[] { "VMXX0007", "VMXX0006", "VMXX0028", "VMXX0009", "VMXX0032" };
        
        public string strDate = "";
        public string strImage = "";
        public string strHiTemp = "";
        public string strLowTemp = "";
        public string strProvince = "";
        string imageThumbnail = "http://image.weather.com/web/common/wxicons/52/";

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
            //    dt.Columns.Add("date", typeof(string));
            //    dt.Columns.Add("image", typeof(string));
            //    dt.Columns.Add("hiTemp", typeof(string));
            //    dt.Columns.Add("lowTemp", typeof(string));
            //    dt.Columns.Add("province", typeof(string));
            //    Session.Add("dtData", dt);
            //    getWeatherInfomation();
            //}
            getWeatherInfomation();
        }
        //private DataTable createData(string strDate, string img, string hiTemp, string lowTemp, string province)
        //{
        //    DataTable dt = (DataTable)Session["dtData"];
        //    dt.Rows.Add(strDate, strImage, hiTemp, lowTemp, province);
        //    return (DataTable)Session["dtData"];
        //}
        private void getWeatherInfomation()
        {
            w1 = new YahooWeather();
            string finalres = "";
            StringBuilder htmlBuilder = new StringBuilder();
            htmlBuilder.Append(@"<table cellpadding='0' cellspacing='1'>");

            foreach (string item in strProvineArr)
            {
                strProvince = dictProvinces[item];
                htmlBuilder.AppendFormat("<tr><td colspan='3'><strong>{0}</strong></td></tr>", strProvince);
                htmlBuilder.AppendFormat("<tr>");
                finalres = w1.getWeather(item);
                try
                {
                    input = XElement.Parse(finalres);
                }
                catch (Exception ex)
                {
                    return;
                }
                
                foreach (XElement el in input.Elements("dayf").Elements("day"))
                {
                    if (el.Attribute("d").Value == "0")
                    {
                        //strHiTemp = el.Element("hi").Value;
                        //strLowTemp = el.Element("low").Value;
                        //strDate = "Today";
                        //if (strHiTemp == "N/A")
                        //{
                        //    strHiTemp = "--";
                        //}
                        var strImage = imageThumbnail + input.Element("cc").Element("icon").Value.ToString() + ".gif";
                        //createData(strDate, strImage, strHiTemp, strLowTemp, strProvince);
                        htmlBuilder.AppendFormat("<td align='center' bgcolor='#F0F0F0'><div class='FixLight'><img src='{1}' style='width:25px; height:25px;'/>{0}<sup>o</sup></div></td>", el.Element("low").Value, strImage, el.Element("hi"));

                    }
                    if (el.Attribute("d").Value == "1")
                    {
                        var strImage = imageThumbnail + input.Element("cc").Element("icon").Value.ToString() + ".gif";
                        htmlBuilder.AppendFormat("<td align='center' bgcolor='#F0F0F0'><div class='FixLight'><img src='{1}' style='width:25px; height:25px;'/>{0}<sup>o</sup></div></td>", el.Element("low").Value, strImage, el.Element("hi"));
                    }
                    if (el.Attribute("d").Value == "2")
                    {
                        var strImage = imageThumbnail + input.Element("cc").Element("icon").Value.ToString() + ".gif";
                        htmlBuilder.AppendFormat("<td align='center' bgcolor='#F0F0F0'><div class='FixLight'><img src='{1}' style='width:25px; height:25px;'/>{0}<sup>o</sup></div></td>", el.Element("low").Value, strImage, el.Element("hi"));
                    }
                }
                htmlBuilder.Append("</tr>");

            }
            htmlBuilder.Append("</table>");
            this.lblWeather.Text = htmlBuilder.ToString();
            //dlData.DataSource = (DataTable)Session["dtData"];
            //dlData.DataBind();
        }
    }
}